﻿#include "lossview.h"
#include "ui_lossview.h"

LossView::LossView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LossView)
{
    ui->setupUi(this);

    model=new QSqlTableModel(this);
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ui->tableView->setModel(model);
}

LossView::~LossView()
{
    delete ui;
}

void LossView::setHeaderData()
{
    model->setHeaderData(0,Qt::Horizontal,QStringLiteral("ID"));
    model->setHeaderData(1,Qt::Horizontal,QStringLiteral("日期"));
    model->setHeaderData(2,Qt::Horizontal,QStringLiteral("名称"));
    model->setHeaderData(3,Qt::Horizontal,QStringLiteral("数量"));
    model->setHeaderData(4,Qt::Horizontal,QStringLiteral("用途"));
    model->setHeaderData(5,Qt::Horizontal,QStringLiteral("备注"));
    model->setHeaderData(6,Qt::Horizontal,QStringLiteral("是否完成？"));
}

void LossView::on_btn_today_clicked()
{
    model->setTable(TB_LOSS);
    model->setFilter(QString("date = '%1'").arg(Env::date));
    model->select();

    setHeaderData();
}

void LossView::on_btn_month_clicked()
{
    QString preDate=Env::month + "-01";
    QString postDate=Env::month + "-31";

    model->setTable(TB_LOSS);
    model->setFilter(QString("date >= '%1' and date <= '%2'").arg(preDate).arg(postDate));
    model->select();

    setHeaderData();
}

void LossView::on_btn_full_clicked()
{
    model->setTable(TB_LOSS);
    model->select();

    setHeaderData();
}

void LossView::on_btn_revert_clicked()
{
    model->revertAll();
}

void LossView::on_btn_submit_clicked()
{
    model->submitAll();
}
